package com.apimarket.controller;

import com.apimarket.entity.SysMenu;
import com.apimarket.entity.SysUser;
import com.apimarket.enums.SecurityEnum;
import com.apimarket.exception.MySecurityException;
import com.apimarket.security.UserDetailsServiceImpl;
import com.apimarket.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/")
public class CasController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("test")
    public User test(HttpServletRequest request){
        AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();
        String username = principal.getName();
        if(StringUtils.isEmpty(username))
            throw new MySecurityException(SecurityEnum.USER_NAME_IS_NUM);
        //根据用户名查询用户信息
        SysUser sysUser = sysUserService.selectByName(username);
        if(sysUser==null)
            throw new MySecurityException(SecurityEnum.USER_IS_NULL);
        //这个集合用于存放用户的权限
        List<GrantedAuthority> grantedAuthorities=new ArrayList<>();
        if(sysUser!=null){
            //获取该用户的所有权限
            List<SysMenu> sysMenus = sysUserService.selectMenuListByUser(sysUser.getId());
            /*if(sysMenus==null||sysMenus.size()==0||sysMenus.get(0)==null)
                throw new MySecurityException(SecurityEnum.USER_HAS_NO_PERMISSION);*/
            //声明用户授权
            if(sysMenus!=null && sysMenus.size()!=0 &&sysMenus.get(0)!=null) {
                sysMenus.forEach(sysMenu -> {
                    //这里如果用户有对应的菜单权限的话，那么这个菜单权限的permission必须每一个都不为null，否则会报错
                    //这里他会自带一个去重
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(sysMenu.getPermission());
                    grantedAuthorities.add(grantedAuthority);
                });
            }
        }
        return new User(sysUser.getUserName(),sysUser.getPassword(),sysUser.getEnabled(),
                sysUser.getNotExpired(),sysUser.getCredentialsNotExpired(),sysUser.getAccountNotLocked(),grantedAuthorities);
    }

    @GetMapping("loginTest")
    public String loginTest(String userName){
        UserDetailsServiceImpl udsi = new UserDetailsServiceImpl();
        UserDetails user = udsi.loadUserByUsername(userName);
        System.out.println(user.getUsername());
        return user.getUsername();
 
    }
}

